 package com.cn.ccp.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractRefreshableApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cn.ccp.service.system.RoleService;
import com.cn.ccp.service.system.RoleService.RoleProportion;

@SuppressWarnings("serial")
public class LoadRoleProportionServlet extends HttpServlet {
	private Logger log = Logger.getLogger(LoadRoleProportionServlet.class);
	
	private RoleService roleService ;
	
	
	public void getStudentService(HttpServletRequest request){
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());//
		 //在servlet中读取applicationContext.xml
		 AbstractRefreshableApplicationContext ctxs = (AbstractRefreshableApplicationContext) ctx;
		 roleService = (RoleService)  ctxs.getBean("roleService");
	}
	
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		getStudentService(request);
		
		 List<RoleProportion> list = roleService.getRoleProportionList();
		 JSONArray array = new JSONArray();
		 for (int i = 0; i < list.size(); i++) {
			JSONObject json = new JSONObject();
//			json.put("country", URLEncoder.encode(list.get(i).getRoleName(),"utf-8"));
			json.put("country", list.get(i).getRoleName());
			json.put("visits", list.get(i).getUserCount());
			array.add(json);
		}
		 response.getWriter().print(array.toString());
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
}
